package com.jingdianjichi.subject.domain.service.impl;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.jingdianjichi.subject.common.enums.IsDeletedEnum;
import com.jingdianjichi.subject.domain.convert.SubjectCategoryConverter;
import com.jingdianjichi.subject.domain.convert.SubjectLabelConverter;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdianjichi.subject.domain.service.SubjectCategoryDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectCategory;
import com.jingdianjichi.subject.infra.basic.entity.SubjectLabel;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectCategoryService;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {

    @Resource
    private SubjectCategoryService subjectCategoryService;;

    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Override
    public void add(SubjectCategoryBO subjectCategoryBO) {
        if(log.isInfoEnabled()){
            log.info("add subjectBO category", JSON.toJSONString(subjectCategoryBO));
        }
        SubjectCategory subjectCategory=SubjectCategoryConverter.INSTANCE.convertBoToCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        subjectCategoryService.insert(subjectCategory);
    }

    @Override
    public List<SubjectCategoryBO> quiryCategory(SubjectCategoryBO subjectCategoryBO) {
        List<SubjectCategory> subjectCategoryList=subjectCategoryService.queryCategory(
                SubjectCategoryConverter.INSTANCE.convertBoToCategory(subjectCategoryBO)
        );
        List<SubjectCategoryBO> boList=SubjectCategoryConverter.INSTANCE.convertCategoryListToCategoryListBo(subjectCategoryList);
        boList.forEach(bo->{
            Integer num=subjectCategoryService.queryCount(bo.getId());
            bo.setCount(num);
        });
        return  boList;
    }

    @Override
    public boolean update(SubjectCategoryBO subjectCategoryBO) {
      int k=subjectCategoryService.update(SubjectCategoryConverter.INSTANCE.convertBoToCategory(subjectCategoryBO));
      if(k==0)
          return false;
      return true;
    }

    public boolean delete(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory=SubjectCategoryConverter.INSTANCE.convertBoToCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(1);
        int k=subjectCategoryService.update(subjectCategory);
        if(k==0)
            return false;
        return true;
    }

    @Override
    public List<SubjectCategoryBO> queryCategoryAndLabel(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory=new SubjectCategory();
        subjectCategory.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        subjectCategory.setParentId(subjectCategoryBO.getId());
        List<SubjectCategory> subjectCategoryList=subjectCategoryService.queryCategory(subjectCategory);
        List<SubjectCategoryBO> boList=SubjectCategoryConverter.INSTANCE.convertCategoryListToCategoryListBo(subjectCategoryList);
        boList.forEach(sc->{
           SubjectMapping subjectMapping=new SubjectMapping();
           subjectMapping.setCategoryId(sc.getId());
           subjectMapping.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
           List<SubjectMapping> subjectMappingList = subjectMappingService.queryLabelId(subjectMapping);
           List<Long> labelIdList=subjectMappingList.stream().map(SubjectMapping::getLabelId).collect(Collectors.toList());
           List<SubjectLabel> labelList=subjectLabelService.batchQueryById(labelIdList);
           sc.setSubjectLabelList(SubjectLabelConverter.INSTANCE.convertCategoryListToCategoryListBo(labelList));
        });
        return boList;
    }
}
